package HibernateOrederManagement.app;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import HibernateOrderManagerment.bean.OrderStatus;

public class OrderStatusDao {
	private Session session;
	public OrderStatusDao(Session session){
		this.session=session;
	}
	public List<OrderStatus> list(){
		Query<OrderStatus> query=session.createQuery("from OrderStatus",OrderStatus.class);
		return query.getResultList();
	}
	public OrderStatus getOrderStatusById(long id) {
		return session.get(OrderStatus.class, id);
	}
	public OrderStatus getByCode(String code){
		Query<OrderStatus> query =session.createQuery("from OrderStatus where code=:code",OrderStatus.class);
		query.setParameter("code", code);
		return query.getSingleResult();
	}
	public OrderStatus getByName(String name){
		Query<OrderStatus> query =session.createQuery("from OrderStatus where name=:name",OrderStatus.class);
		query.setParameter("name", name);
		return query.getSingleResult();
	}
	public OrderStatus listByDescription(String description){
		Query<OrderStatus> query =session.createQuery("from OrderStatus where description=:description",OrderStatus.class);
		query.setParameter("description", description);
		return query.getSingleResult();
	}
	public void insert(OrderStatus orderStatus) {
		Transaction tx = session.beginTransaction();
		try {

			session.save(orderStatus);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
	public void update(OrderStatus orderStatus){
		Transaction tx = session.beginTransaction();
		try {

			session.update(orderStatus);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}
	public void delete(OrderStatus goodsStatus){
		Transaction tx = session.beginTransaction();
		try {
			
			session.delete(goodsStatus);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}

}
